package com.fortune.controller.app;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.controller.AbstractController;
import com.fortune.dal.dao.BankCardDAO;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.BankCard;
import com.fortune.dal.model.Cust;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("appbankcard")
public class AppBankCardController extends AbstractController {

    @Autowired
    private BankCardDAO bankCardDAO;

    @Autowired
    private BankDAO bankDAO;

    @Autowired
    private HttpSession httpSession;


    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("appbankcard");
        List<Map<String,Object>> list = new ArrayList<>();
        Cust u = (Cust) httpSession.getAttribute("user");

        if(u == null){
            throw new BusinessException("未登录，请登录后再操作");

        }
        BankCard param = new BankCard();
        param.setCustId(u.getId());
        List<BankCard> lists = null;
        try {
            lists = bankCardDAO.queryByModel(param);
            if(ListUtils.isNotEmpty(lists)){
                for(BankCard bc : lists){
                    Map<String,Object> map = MapUtils.parseMap(bc);
                    Bank bank = bankDAO.queryById(bc.getBankId());
                    map.put("bankName",bank.getName());
                    map.put("imgUrl",bank.getImgUrl());


                    String code = bc.getCardCode();
                    String simpleCode = code.substring(code.length()-4);
                    map.put("simpleCode",simpleCode);
                    list.add(map);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        mv.addObject("list",list);
        return mv;
    }

    //查看银行卡明细
    @RequestMapping(value = "bankcarddetails", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo(@RequestParam Integer id ) throws BusinessException {
        ModelAndView mv = new ModelAndView("bankcarddetails");
        try {

            BankCard bankCard = bankCardDAO.queryById(id);
            Map<String,Object> map = MapUtils.parseMap(bankCard);
            Bank bank = bankDAO.queryById(bankCard.getBankId());
            if(bank == null){
                throw new BusinessException("出错");
            }

            String cardName = bank.getName();
            map.put("cardName",cardName);
            String imgUrl = bank.getImgUrl();
            map.put("imgUrl",imgUrl);

            String oldCardCode = bankCard.getCardCode();
            String simpleCardCode = oldCardCode.substring(oldCardCode.length() - 4);
            map.put("simpleCardCode",simpleCardCode);

            mv.addObject("bankCard",map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
